package main;

import grid.PlayerGrid;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import player.OverlayMediaPlayer;
import player.Player;
import vk.Video;
import vk.VkAdapter;

import java.io.File;
import java.util.List;

/**
 * @author Eugene Kozobrodov
 */
public class AppWindow {
    private Stage primaryStage;
    public void show(Stage stage) throws Exception {
        this.primaryStage = stage;
        Group root = new Group();
        createWindow(root);
        stage.setTitle("Video Player");
        stage.setWidth(PlayerGrid.SCENE_WIDTH + 20);
        stage.setHeight(PlayerGrid.SCENE_HEIGHT + 60);
        Scene scene = new Scene(root);
        scene.setFill(Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }

    private void createWindow(Group root) {
        VBox sceneMainView = new VBox(10);
        sceneMainView.setPadding(new Insets(50, 10, 10, 10));

        HBox menu = new HBox(10);
        menu.setPadding(new Insets(10));

        Label videoSourceLabel = new Label("Video source: ");
        videoSourceLabel.setTextFill(Color.WHITE);
        menu.getChildren().add(videoSourceLabel);

        ChoiceBox<VideoSourceType> videoSourceChoice = new ChoiceBox<VideoSourceType>();
        videoSourceChoice.getItems().addAll(VideoSourceType.DESKTOP_VIDEO, VideoSourceType.VK, VideoSourceType.STREAM);
        menu.getChildren().add(videoSourceChoice);

        HBox flexiblePart = new HBox(10);
        menu.getChildren().add(flexiblePart);
        setMenuBehavior(videoSourceChoice, flexiblePart, sceneMainView);
        videoSourceChoice.getSelectionModel().select(VideoSourceType.VK); // Select vk by default

        root.getChildren().add(menu);
        root.getChildren().add(sceneMainView);
    }

    private void setMenuBehavior(final ChoiceBox<VideoSourceType> selector, final HBox root, final VBox mainView) {
        selector.valueProperty().addListener(new ChangeListener<VideoSourceType>() {
            public void changed(ObservableValue<? extends VideoSourceType> observableValue, VideoSourceType videoSourceType, VideoSourceType videoSourceType2) {
                root.getChildren().clear();
                mainView.getChildren().clear();
                switch (videoSourceType2) {
                    case DESKTOP_VIDEO:
                        Button btn = new Button("Choose file");
                        btn.setOnAction(new EventHandler<ActionEvent>() {
                            public void handle(ActionEvent actionEvent) {
                                mainView.getChildren().clear();
                                FileChooser fileChooser = new FileChooser();
                                File file = fileChooser.showOpenDialog(primaryStage);
                                if (file != null) {
                                    OverlayMediaPlayer player = new OverlayMediaPlayer();
                                    try {
                                        player.start(mainView, file.toURI().toString());
                                    } catch (Exception e) {
                                        System.err.println(e);
                                    }
                                }
                            }
                        });
                        root.getChildren().add(btn);
                        break;
                    case STREAM:
                        final TextField urlLine = new TextField();
                        urlLine.setPromptText("URL");
                        urlLine.setOnAction(new EventHandler<ActionEvent>() {
                            public void handle(ActionEvent actionEvent) {
                                mainView.getChildren().clear();
                                OverlayMediaPlayer player = new OverlayMediaPlayer();
                                try {
                                    player.start(mainView, urlLine.getText());
                                } catch (Exception e) {
                                    System.err.println(e);
                                }
                            }
                        });
                        root.getChildren().add(urlLine);
                        break;
                    default:
                    case VK:
                        final TextField searchLine = new TextField();
                        searchLine.setPromptText("Search");
                        searchLine.setOnAction(new EventHandler<ActionEvent>() {
                            public void handle(ActionEvent actionEvent) {
                                mainView.getChildren().clear();
                                List<Video> videos = VkAdapter.searchVideos(searchLine.getText());
                                new PlayerGrid(mainView, Player.videosToPlayerList(videos));
                            }
                        });
                        root.getChildren().add(searchLine);
                        break;
                }
                mainView.toBack();
            }
        });
    }
}
